package jp.gr.java_conf.ryz.summarize.android.activity;

import java.util.ArrayList;

import jp.gr.java_conf.ryz.summarize.android.R;
import jp.gr.java_conf.ryz.summarize.android.model.Post;

import org.apache.commons.lang3.text.StrBuilder;

import android.app.Activity;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;

public class GenerateResultActivity extends Activity
{
	public static final String PARAM_POSTS = "param_posts";
	private static final String LINE_SEPARATOR_TAG = "<br />";
	
	private ArrayList<Post> _posts;
	
	
	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_generate_result);
		
		Bundle extras = getIntent().getExtras();
		_posts = (ArrayList<Post>)extras.getSerializable(PARAM_POSTS);
		
		init();
	}
	
	
	private void init()
	{
		StrBuilder mainString = new StrBuilder();
		StrBuilder responseString = new StrBuilder();
		for (Post each : _posts)
		{
			if (each.isMain())
			{
				mainString.append(each.getTitleHtml());
				mainString.appendln(LINE_SEPARATOR_TAG);
				mainString.appendln(each.getBody());
			}
			else
			{
				responseString.append(each.getTitleHtml());
				responseString.appendln(LINE_SEPARATOR_TAG);
				responseString.appendln(each.getBody());
			}
		}
		
		final String main = mainString.toString();
		EditText mainText = (EditText)findViewById(R.id.activity_generate_result_main_part);
		mainText.setText(main);
		
		final String response = responseString.toString();
		EditText responseText = (EditText)findViewById(R.id.activity_generate_result_response_part);
		responseText.setText(response);
		
		Button copyMainButton = (Button)findViewById(R.id.activity_generate_result_copy_main_part);
		copyMainButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				copyToClipBoard(main);
			}
		});
		
		Button copyResponseButton = (Button)findViewById(R.id.activity_generate_result_copy_response_part);
		copyResponseButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				copyToClipBoard(response);
			}
		});
		
//		WebView webview = (WebView)findViewById(R.id.activity_generate_result_webview);
//		webview.loadData("<html><body>" + main + response + "</body></html>", "text/html;charset=UTF-8", "UTF-8");
	}
	
	private void copyToClipBoard(String s)
	{
		ClipboardManager manager = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
		manager.setText(s);
	}

}
